﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Blank.wcf.ClientConnectLib
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T">服务契约</typeparam>
    /// <typeparam name="C">回调契约</typeparam>
    public class DuplexClientBase<T, C> : DuplexClientBase<T> where T : class
    {
        protected DuplexClientBase(InstanceContext<C> context)
            : base(context.Context)
        {

        }
        protected DuplexClientBase(InstanceContext<C> context, string endpointName)
            : base(context.Context, endpointName)
        {

        }

        protected DuplexClientBase(InstanceContext<C> context, Binding binding, EndpointAddress remoteAddress)
            : base(context.Context, binding, remoteAddress)
        {

        }

        static DuplexClientBase()
        {

        }

        internal static void VerifyCallback()
        {
            Type contractType = typeof(T);
            Type callbackType = typeof(C);

            object[] attributes = contractType.GetCustomAttributes(
                typeof(ServiceContractAttribute), false);
            if (attributes.Length == 0)
            {
                throw new InvalidOperationException(
                    string.Format("Type of {0} is not a service Contract", contractType));
            }

            ServiceContractAttribute serviceContractAttribute
                = attributes[0] as ServiceContractAttribute;
            if (callbackType != serviceContractAttribute.CallbackContract)
            {
                throw new InvalidOperationException(
                          string.Format("Type of {0} is not configured as callback Contract for {1}"
                          , callbackType
                          , contractType));
            }
        }
    }
}
